<?php
class ReflectorFormat
{
	function reflect($rawDoc)
	{
		$html = '';

		foreach ($rawDoc->classes as $class)
		{
			$parent = $class->getParentClass();
			$parentName =( $parent ) ? ' Extends: <span style="color: orange;">'.$parent->name.'</span>':'';

			$html .= '<h1>';
			$html .= sprintf(
               "%s%s%s %s" ,
			$class->isInternal() ? 'internal' : 'user-defined',
			$class->isAbstract() ? ' abstract' : '',
			$class->isFinal() ? ' final' : '',
			$class->isInterface() ? 'Interface' : 'Class'
			);
			#			$html .= '<h1>';
			$html .= ' <span style="color: blue;">'.$class->getName().'</span>'.$parentName;
			$html .= '</h1>';
			$html .= '<pre>'.$class->getDocComment().'</pre>';
			$html .= NL.'<h2>Properties</h2>';

			$comment = explode(NL, $class->getDocComment());
			$searches = array('static', 'subpackage', 'since');
			$subPackage = '';
			foreach ($comment as $c)
			{
				foreach ($searches as $search)
				{
					if( strpos($c, '@'.$search))
					{
						#$wikiClassPage .= $c.NL;;
						if( $search == 'subpackage')
						{
							$p =  strpos($c, $search);
							$subPackage = trim(substr($c, strpos($c, $search)+strlen($search)));
						}
					}
				}
			}//foreach

			$subPackage =( $subPackage ) ? $subPackage : $class->subPackage;

			$class->subPackage = $class->subPackage;
			$class->subPackageName = $class->subPackageName;
			$class->subSubPackage = $class->subSubPackage;
			#			$class->subPackageName = $subPackage;

			$properties = $class->getProperties();
			foreach ($properties as $prop)
			{
				$property = $class->getProperty($prop->name);

				$html .= BR.sprintf(
                  "%s%s%s%s property <strong>%s</strong>",
				$property->isPublic() ? ' <strong style="color: green">public</strong>' : '',
				$property->isPrivate() ? ' <strong style="color: orange">private</strong>' : '',
				$property->isProtected() ? ' <strong style="color: red">protected</strong>' : '',
				$property->isStatic() ? ' <strong style="color: black">static</strong>' : '',
				$property->getName()

				);
			}//foreach

			$cMethods =  $class->getMethods();

			$indent = 0;
			$displayClassName = '';
			foreach ($cMethods as $cMethod)
			{
				$mPath = $cMethod->getFileName();

				//--$this class or $that class ;)
				//--..base or extended
				//..also marks the extended extended classes orange.. TODO !
				#$color =( $mPath == $fullPathFileName ) ? 'green' : 'orange';
				$titel = sprintf(
                  "%s%s%s%s%s%s Method <strong style='color: orange;'>%s</strong>",
				$cMethod->isAbstract() ? ' abstract' : '',
				$cMethod->isFinal()       ? ' final' : '',
				$cMethod->isPublic()      ? ' <strong style="color: green">public</strong>' : '',
				$cMethod->isPrivate()  ? ' <strong style="color: orange">private</strong>' : '',
				$cMethod->isProtected()   ? ' <strong style="color: red">protected</strong>' : '',
				$cMethod->isStatic()      ? ' <strong style="color: black">static</strong>' : '',
				$cMethod->getName()
				);
				$pClass= $cMethod->getDeclaringClass();
				$declaringClass = $pClass->getName();
				if( $declaringClass != $displayClassName )
				{

					$indent++;
					$html .= '<h1>';
					#  echo str_repeat("&nbsp;&nbsp;", $indent);
					$html .= ( $displayClassName ) ? '<span style="color: orange">Extends</span>&nbsp;'.$declaringClass : $declaringClass;
					$html .= '</h1>';
					$displayClassName = $declaringClass;
					$html .= NL.'<h2>Methods</h2>';

				}
				$paramString = array();
				$parameters = $cMethod->getParameters();
				foreach( $parameters as $parameter )
				{
					$s = '';
					$s .= sprintf("%s<strong style='color: brown;'>$%s</strong>",
					#                     $parameter->isOptional() ? '<strong style="color: blue;">optional</strong> ' : '',
					$parameter->isPassedByReference() ? '<strong style="color: blue;"> & </strong>' : '',
					$parameter->getName()
					);

					if( $parameter->isDefaultValueAvailable())
					{
						$def = $parameter->getDefaultValue();
						if( $def === null)
						{
							$s .= '=NULL';
						}
						else if( $def === false )
						{
							$s .= '=FALSE';
						}
						else if( $def === true )
						{
							$s .= '=TRUE';
						}
						else if( $def === array() )
						{
							$s .= '=array()';
						}

						else if( $def === '' )
						{
							$s .= '=\'\'';
						}
						else
						{
							$s .= '='.$parameter->getDefaultValue();
						}
					}

					$paramString[] = $s;

				}//foreach
				$paramString = implode(', ', $paramString);
				$html .= $titel.'<strong style="color: blue;">'.$cMethod->name.'</strong>( '.$paramString.' )';
				$html .= '<br />'.'Lines'.' # '.$cMethod->getStartLine().' - '.$cMethod->getEndLine();

				if( strtolower($declaringClass) == strtolower($class->getName()) )
				{
					$comment = explode(NL, $cMethod->getDocComment());
					$searches = array('@return', '@since');
					$syntaxAdds = '';
					$hasReturn = false;
					foreach ($comment as $c)
					{
						foreach ($searches as $search)
						{
							if( strpos($c, $search))
							{
								$c = str_replace($search, "'''".$search."'''", $c);
								$syntaxAdds .= trim($c).NL;
								if( $search == '@return'){ $hasReturn = true; }
							}
						}
					}//foreach

				}
				$comment = $cMethod->getDocComment();
				if( $comment )
				{
					$html .= NL.'<div style="border: 1px dotted grey; background-color: white;" class="doccomment_doccomment">';
					$html .= nl2br($comment);
					$html .= '</div>';
				}
				$fileLocation = substr($cMethod->getFileName(), strlen(JPATH_SITE)).'<br />'.'Lines'.' # '.$cMethod->getStartLine().' - '.$cMethod->getEndLine();

			}//foreach methods

		}//foreach classes


		return $html;
	}//function

}//class
